package org.iwidget.desktop.model;
import org.w3c.dom.*;

/**
 *
 * @author Muhammad Hakim A
 */

public class ActionElement
{

    public ActionElement()
    {
        name = null;
        file = null;
        type = null;
        interval = 0.0F;
    }

    @Override
    public void finalize()
        throws Throwable
    {
        file = null;
        name = null;
        script = null;
        type = null;
        super.finalize();
    }

    public String getFile()
    {
        return file;
    }

    public void setFile(String string)
    {
        file = string;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String string)
    {
        name = string;
    }

    public String getType()
    {
        return type;
    }

    public void setType(String string)
    {
        type = string;
    }

    public int getIntervalInMilliseconds()
    {
        return (int)(interval * 1000F);
    }

    public float getInterval()
    {
        return interval;
    }

    public void setInterval(float f)
    {
        interval = f;
    }

    public String getScript()
    {
        return script;
    }

    public void setScript(String string)
    {
        script = string;
    }

    public void addChildAction(String actionType, Node actionNode)
    {
        setName(actionType);
        NodeList nodes = actionNode.getChildNodes();
        for(int iCtr = 0; iCtr < nodes.getLength(); iCtr++)
        {
            Node childNode = nodes.item(iCtr);
            String nodeName = childNode.getNodeName();
            String nodeValue = childNode.getNodeValue();
            if(nodeName.equals("type"))
            {
                setType(childNode.getFirstChild().getNodeValue());
                continue;
            }
            if(nodeName.equals("file"))
            {
                setFile(childNode.getFirstChild().getNodeValue());
                continue;
            }
            if(nodeName.equals("interval"))
            {
                setInterval(Integer.parseInt(childNode.getFirstChild().getNodeValue()));
                continue;
            }
            if(nodeName.equals("script"))
                setScript(childNode.getFirstChild().getNodeValue().trim());
        }

    }

    public void parse(Node node)
        throws Exception
    {
        ActionElement action = new ActionElement();
        NamedNodeMap map = node.getAttributes();
        for(int iCtr = 0; iCtr < map.getLength(); iCtr++)
        {
            Node attr = map.item(iCtr);
            parseHelper(attr.getNodeName(), attr.getNodeValue());
        }

        NodeList nodes = node.getChildNodes();
        for(int iCtr = 0; iCtr < nodes.getLength(); iCtr++)
        {
            Node childNode = nodes.item(iCtr);
            String nodeName = childNode.getNodeName();
            String nodeValue = childNode.getNodeValue();
            if(childNode.getFirstChild() != null)
                parseHelper(nodeName, childNode.getFirstChild().getNodeValue());
        }

    }

    private void parseHelper(String nodeName, String value)
    {
        if(nodeName.equals("type"))
            setType(value);
        else
        if(nodeName.equals("file"))
            setFile(value);
        else
        if(nodeName.equals("interval"))
            setInterval(Float.parseFloat(value));
        else
        if(nodeName.equals("script"))
            setScript(value.trim());
        else
        if(nodeName.equals("trigger"))
            setType(value);
    }

    public static final String ACTION_ON_DRAG_ENTER = "onDragEnter";
    public static final String ACTION_ON_DRAG_EXIT = "onDragExit";
    public static final String ACTION_ON_DRAG_DROP = "onDragDrop";
    public static final String ACTION_ON_GAIN_FOCUS = "onGainFocus";
    public static final String ACTION_ON_KEY_DOWN = "onKeyDown";
    public static final String ACTION_ON_KEY_UP = "onKeyUp";
    public static final String ACTION_ON_KEY_PRESS = "onKeyPress";
    public static final String ACTION_ON_LOSE_FOCUS = "onLoseFocus";
    public static final String ACTION_ON_LOAD = "onLoad";
    public static final String ACTION_ON_MAY_CHANGE_PREFERENCES = "onMayChangePreferences";
    public static final String ACTION_ON_MOUSE_UP = "onMouseUp";
    public static final String ACTION_ON_MOUSE_DOWN = "onMouseDown";
    public static final String ACTION_ON_MOUSE_ENTER = "onMouseEnter";
    public static final String ACTION_ON_MOUSE_EXIT = "onMouseExit";
    public static final String ACTION_ON_PREFERENCES_CHANGED = "onPreferencesChanged";
    public static final String ACTION_ON_TIMER = "onTimer";
    public static final String ACTION_ON_UNLOAD = "onUnload";
    public static final String ACTION_ON_SYSTEM_MOUSE_MOVE = "onSystemMouseMove";
    public static final String ACTION_ON_RUN_IN_BG_COMPLETE = "onRunInBgComplete";
    public static final String ACTION_ON_SCROLL_COMPLETE = "onScrollComplete";
    private String name;
    private String file;
    private String type;
    private String script;
    private float interval;
}